package com.mikufufu.blog.controller.admin;

import com.mikufufu.blog.common.entity.AjaxResult;
import com.mikufufu.blog.model.entity.Storage;
import com.mikufufu.blog.service.StorageService;
import com.mikufufu.blog.service.SysSettingService;
import com.mikufufu.blog.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/storage")
@Api(tags = "存储桶接口")
public class StorageAdminController {

    @Autowired
    private StorageService storageService;
    @Autowired
    private SysSettingService sysSettingService;

    @GetMapping("/getStorageSetting")
    @ApiOperation(value = "获取系统设置的存储桶详情")
    public AjaxResult<Storage> storage(){
        String storageCode = sysSettingService.getStorageCode();
        return AjaxResult.data(storageService.getStorage(storageCode));
    }

    @GetMapping("/getStorageInfo")
    @ApiOperation(value = "获取指定存储桶的存储服务商详情")
    @ApiImplicitParam(name = "storageCode", value = "存储桶编码", required = true)
    public AjaxResult<Storage> storageInfo(String storageCode){
        if (StringUtils.isBlank(storageCode)){
            return AjaxResult.error("存储服务商编码不能为空");
        }
        return AjaxResult.data(storageService.getStorage(storageCode));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取存储桶列表")
    public AjaxResult<List<Storage>> list(){
        return AjaxResult.data(storageService.list());
    }

    @PostMapping("/updateStorageSetting")
    @ApiOperation(value = "更新系统默认的存储桶")
    public AjaxResult<String> storage(@RequestBody Storage storage){
        boolean flag;
        String storageCode = storage.getStorageCode();
        if(StringUtils.isBlank(storageCode)){
            return AjaxResult.error("存储服务商编码不能为空");
        }
        flag = sysSettingService.updateStorageCode(storageCode);
        if (flag){
            return AjaxResult.status(storageService.updateStorage(storage),"存储服务商修改成功","存储服务商修改失败");
        }else {
            return AjaxResult.error("存储服务商设置修改失败");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除存储桶")
    @ApiImplicitParam(name = "storageCode", value = "存储桶编码", required = true)
    public AjaxResult<Boolean> delete(String storageCode){
        if (StringUtils.isBlank(storageCode)){
            return AjaxResult.error("存储服务商不能为空");
        }
        return AjaxResult.status(storageService.deleteStorage(storageCode));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增存储桶")
    public AjaxResult<Boolean> add(@RequestBody Storage storage){
        if (StringUtils.isBlank(storage.getStorageCode())) {
            return AjaxResult.error("存储服务商不能为空");
        }
        if (StringUtils.isBlank(storage.getStorageName())) {
            return AjaxResult.error("存储服务商名称不能为空");
        }
        if (StringUtils.isBlank(storage.getHost())) {
            return AjaxResult.error("存储服务商域名不能为空");
        }
        if (StringUtils.isBlank(storage.getAccessKey())) {
            return AjaxResult.error("存储服务商AccessKey不能为空");
        }
        if (StringUtils.isBlank(storage.getSecretKey())) {
            return AjaxResult.error("存储服务商SecretKey不能为空");
        }
        return AjaxResult.status(storageService.save(storage));
    }

}
